package com.gcb.controller.ship;

import com.gcb.common.annotation.Log;
import com.gcb.common.core.domain.PageResult;
import com.gcb.common.core.domain.R;
import com.gcb.common.utils.poi.ExcelUtil;
import com.gcb.domain.dto.ShipQueryDTO;
import com.gcb.domain.dto.ShipSaveDTO;
import com.gcb.domain.vo.ShipVO;
import com.gcb.framework.utils.SecurityUtils;
import com.gcb.service.IShipService;
import com.github.pagehelper.PageHelper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/ship")
public class ShipController {

    @Resource
    private IShipService shipService;

    /**
     * 203.查询船舶列表接口
     */
    @PreAuthorize("@ss.hasPermi('platform:ship:page')")
    @GetMapping("/page")
    public PageResult getPageList(ShipQueryDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<ShipVO> pageList = shipService.pageList(dto);
        return PageResult.ok(pageList);
    }

    /**
     * 根据企业id查船舶列表
     * @param companyId 企业id
     * @return 船舶列表
     */
    @GetMapping("/list/by/companyId")
    public R<List<ShipVO>> getListByCompany(@RequestParam Long companyId) {
        return R.ok(shipService.getShipListByCompanyId(companyId));
    }

    /**
     * 201.新增船舶接口
     *
     * @param dto
     * @return
     */
    @Log
    @PreAuthorize("@ss.hasPermi('platform:ship:add')")
    @PostMapping("/add")
    public R<String> addShip(@RequestBody ShipSaveDTO dto) {
        shipService.save(dto, SecurityUtils.getUserId());
        return R.ok();
    }

    /**
     * 206.修改船舶接口
     *
     * @param dto
     * @return
     */
    @Log
    @PreAuthorize("@ss.hasPermi('platform:ship:edit')")
    @PostMapping("/edit")
    public R<String> updateShip(@RequestBody ShipSaveDTO dto) {
        shipService.update(dto, SecurityUtils.getUserId());
        return R.ok();
    }

    /**
     * 205.批量/单个修改船舶归属公司接口
     *
     * @param idList    id集合
     * @param companyId 归属公司id
     * @return
     */
    @Log
    @PreAuthorize("@ss.hasPermi('platform:ship:updateShipCompany')")
    @GetMapping("/edit/company")
    public R<String> updateShipCompany(Long[] idList, Long companyId) {
        shipService.updateBatchCompany(idList, companyId);
        return R.ok();
    }

    /**
     * 202.查询船舶详情接口
     *
     * @param id id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('platform:ship:detail')")
    @GetMapping("/detail")
    public R<ShipVO> getDetail(@RequestParam String id) {
        ShipVO detail = shipService.detail(id);
        return R.ok(detail);
    }

    @Log
    @PreAuthorize("@ss.hasPermi('platform:ship:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShipQueryDTO dto) {
        List<ShipVO> pageList = shipService.pageList(dto);
        ExcelUtil<ShipVO> util = new ExcelUtil<>(ShipVO.class);
        util.exportExcel(response, pageList, "船舶列表");
    }

}
